
# Fighter.rb
# Implements a DynamicObject to act as a Fighter object.

require 'game/DynamicObject'
require 'game/Bullet'
require 'ai/Statistics'
require 'game/ResponseModel'
require 'game/SensorModel'

class Fighter < DynamicObject

  include Statistics
  include ResponseModel
  include SensorModel

  attr_reader   :bullets, :window, :data, :alive

  def initialize(holder, options={})
    super(holder, options)

    @window  = holder
    @image   = Gosu::Image.new(holder, options['image'], false)

    @bullets = []
    @bullet_options = options['bullet']
    @bullet_burst   = @bullet_options['burst']

    @damage_recived = @damage_produced = @shots = 0
    @alive = true
    @time  = Time.now
    @data  = { :recived  => @damage_recived,
      :produced => @damage_produced,
      :shots    => @shots,
      :time     => @time
    }	
  end

  def inc_stat(*types)
    types.each { |value| @data[value] += 1 }
  end

  # Perform this action when the object is no longer necesary.
  def destroy
    return if not @alive
    super

    @alive       = false
    @data[:time] = Time.now - @data[:time]
  end

  # Reaction to an impact of a bullet.
  def react_to(object)
    @vel_x += object.vel_x / 3
    @vel_y += object.vel_y / 3

    inc_stat(:recived)
  end

  # Move yourself backwards when fire a shoot.
  def bounce_back
    @vel_x += Gosu::offset_x((180 + @angle), @bullet_burst / 5)
    @vel_y += Gosu::offset_y((180 + @angle), @bullet_burst / 5)
  end

  # Draw the path of a chain of bullets.
  def direct_motion
    @bullets.each do |bullet| 
      bullet.move
      bullet.draw 
    end

    @bullets.reject! { |bullet| bullet.out? }    
    move
  end

  # Move the object to its next position (needed by Gosu::Window).
  def move
    super

    # Make the fighter stops smoothly.   
    @vel_x *= 0.95
    @vel_y *= 0.95
  end

end
